﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace vAddins
{
    public class vAddin
    {
        internal vAddin(string directory)
        {
            Folder = directory;

            Manifest = new Manifest(Path.Combine(directory, Manifest.FileName));

            foreach (var r in Manifest.References)
            {
                switch (r.Type)
                {
                    case ReferenceType.Absolute:
                        _absReferences.Add(r.Path);
                        break;
                    case ReferenceType.System:
                        _absReferences.Add(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Reference Assemblies", "Microsoft", "Framework", ".NETFramework", "v4.0", "Profile", "Client", r.Path));
                        break;
                    case ReferenceType.Known:
                        _absReferences.Add(Path.Combine(directory, r.Path));
                        break;
                    case ReferenceType.Host:
                         
                        break;
                }
            }
        }

        internal void Build()
        {
            Assembly = Builder.Build(Folder, Language.CSharp, _absReferences);
        }

        public static implicit operator Assembly(vAddin a)
        {
            return a.Assembly;
        }

        public static implicit operator vAddin(Assembly a)
        {
            try
            {
                return vAddinsEngine.LoadedAddins.Where(va => va.Assembly == a).First();
            }
            catch { return null; }
        }

        internal Manifest Manifest;
        internal Assembly Assembly;

        internal List<string> _absReferences = new List<string>();
        public List<string> AbsoluteReferences { get { return _absReferences; } }

        public string Folder { get; internal set; }
    }
}
